package com.jsb.laundry.service.impl;

import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.dao.UserInfoDao;
import com.jsb.laundry.model.dto.CouponRecord;
import com.jsb.laundry.model.entity.UserDetail;
import com.jsb.laundry.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @author zmy
 * @create 2023-07-30 23:04
 */
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoDao userInfoDao;

    @Override
    public String getUserIdByOpenid(String openid) {
        return userInfoDao.getUserIdByOpenid(openid);
    }

    @Override
    public String getOpenIdByUserId(String appUserId) {
        return userInfoDao.getOpenIdByUserId(appUserId);
    }

    /**
     * 新人领取 邀请人赠送的优惠券
     *
     * @param couponRecord
     * @return
     */
    @Override
    public int getShareCoupon(CouponRecord couponRecord) {
        //把赠送人赠送的券状态修改为已领取
        int var0 = userInfoDao.updShareCoupon(couponRecord);
        if (var0 == 0) {
            return 0;
        }
        //查询赠送的这张优惠券的信息
        CouponRecord couponInfo = userInfoDao.getCouponInfoById(couponRecord.getCouponId());
        couponInfo.setUserId(couponRecord.getUserId());
        couponInfo.setPayId("SHARE-" + JSBUtil.generateUUID());
        couponInfo.setCreateTime(new Date());
        couponInfo.setPayTime(new Date());
        couponInfo.setBeginTime(new Date());
        couponInfo.setEndTime(Date.from(LocalDate.now().plusMonths(1).atStartOfDay(ZoneId.systemDefault()).toInstant())); //领取的优惠券有效期 从当前 添加一个月
        couponInfo.setUseType("0");
        couponInfo.setGetType("2");

        //添加一条优惠券给领取人
        int var1 = userInfoDao.addGetShareCoupon(couponInfo);
        return var1;
    }

    @Override
    public UserDetail getUserDetailByUserId(String userId) {
        return userInfoDao.getUserDetail(userId);
    }
}
